define(function (require, exports, module) {
	
	var HandlerBars = require("handlebars");
	var productSvc = require("../store/productSvc");
	var itemsTpl = require("../assets/tpl/items.tpl");
	var store = require("store");
	var activeUser = store.get("user");
	
    var instance = {
    	//初始化服务项目列表
        initServiceItems: function (widget,typeData) {
        	$(".product-items").html("");
        	productSvc.queryServiceItems(function(data){
        		var datas = [];
        		var serviceItemData = [];
        		for(var i=0;i<typeData.length;i++){
        			var arr = $.map(data,function(n,j){
        				if(n.serviceType && n.serviceType.serviceClass && n.serviceType.serviceClass.pkServiceClass == typeData[i].pk){
        					return {
        						pk:n.serviceType.pkServiceType,
        						image:"api/attachment/servicetype/typePicture_"+n.serviceType.pkServiceType,
        						name:n.serviceType.name,
        						price:n.price,
        						content: n.serviceType.content,
        						promoPrice:n.promoPrice,
        						unit:n.serviceType.unit.value,
        						type:"service",
        						parent:{
        							pk:typeData[i].pk,
        	        				name:typeData[i].name,
        						}
        					};
        				}
        			});
        			datas.push({
        				pk:typeData[i].pk,
        				name:typeData[i].name,
        				item:arr
        			});
        			$("#num_"+typeData[i].pk).text(arr.length);
        			$.each(arr,function(i,n){
        				serviceItemData.push(n);
        			});
        		}
        		widget.set("serviceItemData",serviceItemData);
        		var html = HandlerBars.compile(itemsTpl)({itemgruop:datas});
        		$(".product-items").html(html);
        		
        	});
        	
        },
        //初始化商品列表
        initGoodsItems:function(widget,typeData){
        	$(".product-items").html("");
        	productSvc.queryGoodsItems(function(data){
        		var datas = [];
        		var goodsItemData = [];
        		for(var i=0;i<typeData.length;i++){
        			var arr = $.map(data,function(n,j){
        				if(n.goods && n.goods.goodsType && n.goods.goodsType.pkGoodsType == typeData[i].pk){
        					return {
        						pk:n.goods.pkGoods,
        						image:"api/attachment/goods/goodsPicture_"+n.goods.pkGoods,
        						name:n.goods.name,
        						price:n.price,
        						content: n.goods.description,
        						promoPrice:n.promoPrice,
        						unit:n.goods.unit.name,
        						type:"goods",
        						parent:{
        							pk:typeData[i].pk,
        	        				name:typeData[i].name,
        						}
        					};
        				}
        			});
        			datas.push({
        				pk:typeData[i].pk,
        				name:typeData[i].name,
        				item:arr
        			});
        			$("#num_"+typeData[i].pk).text(arr.length);
        			$.each(arr,function(i,n){
        				goodsItemData.push(n);
        			});
        		}
        		
        		widget.set("goodsItemData",goodsItemData);
        		var html = HandlerBars.compile(itemsTpl)({itemgruop:datas});
        		$(".product-items").html(html);
        		
        	});
        },
        // 初始化菜品列表
        initDishItems:function(widget,typeData){
        	$(".product-items").html("");
        	productSvc.queryDishItems(function(data){
        		var datas = [];
        		var dishItemData = [];
        		for(var i=0;i<typeData.length;i++){
        			var arr = $.map(data,function(n,j){
        				if(n.dish && n.dish.dishType && n.dish.dishType.pkDishType == typeData[i].pk){
        					return {
        						pk:n.dish.pkDish,
        						image:"api/attachment/cater/dish/"+activeUser.servicePoint.pkServicePoint+"/"+n.dish.pkDish,
        						name:n.dish.name,
        						price:n.price,
        						content: n.dish.remark,
//        						promoPrice:n.price,
        						unit:"份",
        						type:"dish",
        						parent:{
        							pk:typeData[i].pk,
        	        				name:typeData[i].name,
        						}
        					};
        				}
        			});
        			datas.push({
        				pk:typeData[i].pk,
        				name:typeData[i].name,
        				item:arr
        			});
        			$("#num_"+typeData[i].pk).text(arr.length);
        			$.each(arr,function(i,n){
        				dishItemData.push(n);
        			});
        		}
        		
        		widget.set("dishItemData",dishItemData);
        		var html = HandlerBars.compile(itemsTpl)({itemgruop:datas});
        		$(".product-items").html(html);
        		
        	});
        }
    };
    module.exports = instance;
});
